﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using PumpCentralInterfaces;

namespace PumeCentralService
{
    [ServiceBehavior(InstanceContextMode= InstanceContextMode.PerCall, ConcurrencyMode= ConcurrencyMode.Reentrant)]// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class PumpCentralService : IPumpCentalService
    {
        private static Dictionary<string,IPumpCentralCallback> pumps = new Dictionary<string,IPumpCentralCallback>();
        private static IPumpCentralCallback central;
        
        public void SendData()
        {
            // Obsolete
        } 

        public void RaiseAlarm()
        {
            // nåede ikke at blive implementeret
        } 

        public bool Subscribe()
        {
            try
            {                
                    if(central == null)
                    {
                        central = OperationContext.Current.GetCallbackChannel<IPumpCentralCallback>();
                        return true;
                    }
                    else
                    {
                       return false;
                    }             
            }
            catch (Exception e)
            {
                return false;
            }
        } // skal kaldes fra centralen

        public bool UnSubscribePump(PumpStatusDTO dto)
        {
                if (pumps.ContainsKey(dto.ID))
                {
                    pumps.Remove(dto.ID);
                    return true;
                }
                return false;
        }
        
        public bool RegisterPump(PumpStatusDTO dto)
        {
            //if (((ICommunicationObject)central).State == CommunicationState.Opened)
            //{
            //    if (central.RegisterPump(dto))
            //    {
            //        return true;
            //    }
            //    else
            //    { 
            //        return false; 
            //    }
            //}

            return false;
        }

        public bool SubscribeFromPump(PumpStatusDTO pumpdata)
        {
            IPumpCentralCallback callback = OperationContext.Current.GetCallbackChannel<IPumpCentralCallback>();
            string id = pumpdata.ID;
            
                pumps.Add(pumpdata.ID, callback);
                RegisterPump(pumpdata);
                return true;
            
        }

        public bool UnSubscribeCentral()
        {            
            central = null;
            return true;
        }
        
        public List<PumpStatusDTO> GetDataFromAllPumps()
        {
            List<PumpStatusDTO> dtos = new List<PumpStatusDTO>();
            foreach (var KeyValuePair in pumps)
            {
                if (((ICommunicationObject)KeyValuePair.Value).State == CommunicationState.Opened)
                {
                    dtos.Add(KeyValuePair.Value.GetData());
                }
                else
                {
                    dtos.Add(null);
                }
            }

            return dtos;
        }
        
        public PumpStatusDTO GetDataFromSinglePump(string ID)
        {
            IPumpCentralCallback callback;

            pumps.TryGetValue(ID, out callback);

            if (callback != null)
            {
                return callback.GetData();
            }

            else
            {
                return null;
            }
        }
    }
}
